/**
 * Created by CL on 2017/4/26.
 */
$(".icon-back").click(function () {
    window.location.href="codemain.html";
})
$("#createbtn").click(function () {
    var name=$("#name").val();
    var thr=$("#threshold").val();
    if (check(name,thr)) {
        $("#loadingToast").fadeToggle(250);
        setTimeout(create, 1000)
    }
});
function check(name,thr) {
    if(name==''||thr==''){
        $(".failed").html("名称或阀值不能为空");
        $("#failtoast").fadeToggle(250);
        setTimeout(failtoggle,700);
        return false;
    }
    else if(thr.length>3){
        $(".failed").html("阀值超过长度");
        $("#failtoast").fadeToggle(250);
        setTimeout(failtoggle,700);
        return false;
    }
    else if(isNaN(thr)){
        $(".failed").html("阀值必须为数字");
        $("#failtoast").fadeToggle(250);
        setTimeout(failtoggle,700);
        return false;
    }
    else {
        return true;
    }
}
function create () {
    var name=$("#name").val();
    var thr=$("#threshold").val();
    $.ajax(
        {
            type:"post",
            url:top_url+"hmcode/create",
            dataType:"json",
            data:{
                token:sessionStorage.getItem('user'),
                hmName:name,
                threshold:thr
            },
            success:function (data) {
                console.log(data);
                $("#loadingToast").fadeToggle(250);
                if(data.status == '200'){
                    $("#toast").fadeToggle(250);
                    sessionStorage.setItem('hmInfo',JSON.stringify(data.payload));
                    sessionStorage.setItem('hid',data.payload.hid)
                    setTimeout(toasttoggle,700);
                    setTimeout(href,1000);
                }
                if (data.status == '400') {
                    $(".failed").html(data.err);
                    $("#failtoast").fadeToggle(250);
                    setTimeout(failtoggle, 700)
                }

            },
            error:function (XMLHttpRequest, textStatus, errorThrown) {
                console.log(XMLHttpRequest, textStatus, errorThrown)
            }
        }

    )
}